import Show from 'common/show.js'
import {
	apiBaseURL
} from './config.js';

// 异步请求
function request(url, method, data, header = {}) {
	// let location = uni.getStorageSync('location') ? uni.getStorageSync('location') : '';
	uni.showLoading({ title: '正在加载'});
	let pages = getCurrentPages()
	// console.log(pages)
	return new Promise((resolve, reject) => {
		uni.request({
			url: apiBaseURL + url,
			data: {
				...data,
			},
			header: header,
			method: method,
			success: (res) => {
				// console.log(res)
				if (res.data.code == 200) {
					resolve(res.data);
				} else {
					uni.showToast({
						title:res.data.msg,
						icon:"none",
						duration:1500
					})
					if (res.data.code == 1006) {
						uni.clearStorageSync()
						if (pages.length>0) {
							let redirectUrl = pages[pages.length-1].__page__.fullPath;
							setTimeout(() => {
								uni.navigateTo({
									url:`/pages/login/login?redirectUrl=${redirectUrl}`
								})
							},800)
						} else {
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/login/login'
								})
							},800)
						}
					}
					resolve(res.data)
					// reject(res.data);
				} 
			},
			fail: (error) => {
				return uni.showToast({
					title:error.errMsg,
					icon:"none",
					duration:1500
				})
			},
			complete: () => {
				setTimeout(function () {
					uni.hideLoading();
				}, 500);
			}
		})
	}).catch((error)=>{
		console.error("error",error);
	})
}

//get
export const get = (url, data) => {
	return request(url, 'get',data, {
		'content-type': 'application/json'
	});
}

//post
export const post = (url, data) => {
	return request(url, 'post',data, {
		// 'X-App-Version': uni.getStorageSync("X-User-Version") || "1",//版本号
		// 'X-User-Agent':'HC20001',//固定值
		'Accept':'application/json',
		'content-type': 'application/json',//类型固定值
		'token':uni.getStorageSync("token") || '',
		// "Content-Type": "application/x-www-form-urlencoded", //formdata 格式的上传
		// 'X-User-Id':uni.getStorageSync("X-User-Id") || '',//初始化后的 app_uid
		// 'X-App-Sid':uni.getStorageSync("X-App-Sid") || '',//登录后的 X-App-Sid
	});
}

const showError = code => {
	let errorMsg = ''
	switch (code) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${apiBaseURL}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = '请求失败'
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1500,
		complete: function () {
			setTimeout(function () {
				uni.hideToast();
			}, 1500);
		}
	});
}
// 上传图片
export const uploadImage = (apiurl, filepath, callback) => {
	uni.showLoading({ title: '上传中...'});
	let header = {
		'access_token': uni.getStorageSync('access_token'),
		'content-type': 'text/plain'
	}
	uni.uploadFile({
		url: apiBaseURL + apiurl,
		// url:"https://api.mtyzh.com/owl-main/common/upload.json",
		filePath: filepath,
		fileType: 'image',
		header: header,
		name: 'upfile',
		success: (uploadFileRes) => {
			uni.hideLoading();
			// Show.success('头像修改成功！')
			callback(JSON.parse(uploadFileRes.data));
		},
		fail: (error) => {
			uni.hideLoading();
			// Show.show('头像修改失败！')
		},
		complete: () => {},
	});
}
